package com.hunk.taopeili;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description :
 * 	Servlet implementation class MsgServlet
 * @author HunkDeng
 * @since 2013-1-28
 */
public class MsgServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MsgServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("MsgServlet post");
		request.setCharacterEncoding("utf-8");    
		String name = trans(request.getParameter("name"));
		String email = trans(request.getParameter("email"));
		String msg = request.getParameter("message");
		
		EmailUtils.sendMsg(name, msg, email);
		EmailUtils.sendMsg(name, msg, "niknow@163.com");
		
		ServletContext sc = getServletContext(); 

        RequestDispatcher rd = null; 

        rd = sc.getRequestDispatcher("/send_success.html");       //定向的页面 

        rd.forward(request, response); 
	}
	
	private String trans(String str) throws UnsupportedEncodingException {
		return new String(str.getBytes("GBK"));
	}

}
